import axios from "axios";
// import {
//   ElMessage,
// } from 'element-plus'
// import router from '@/router/index.js'
// import jwt_decode from "jwt-decode";

const BASE_URL = "http://localhost:3004";
const requests = axios.create({
  baseURL: BASE_URL,
  timeout: 5000,
});
//请求拦截器
requests.interceptors.request.use((config) => {
  config.headers = {
    // Authorization: "Bearer " + localStorage.getItem(""),
    "Content-Type": config.ContentType || "application/json",
  };
  return config;
});
//响应拦截器
requests.interceptors.response.use(
  (res) => {
    return res.data;
  },
  (err) => {
    if (err.response) {
      if (
        err.response.status === 400 ||
        err.response.status === 401 ||
        err.response.status === 422
      ) {
        // ElMessage.closeAll();
        // ElMessage({
        //   message: err.response.data.message,
        //   type: "error",
        //   duration: 3000,
        // });
      }

      if (err.response.status === 403) {
        // ElMessage.closeAll();
        // ElMessage({
        //   message: err.response.data.message,
        //   type: "error",
        //   duration: 3000,
        // });
      }
      if (err.response.status === 500 || err.response.status === 404) {
        // ElMessage.closeAll();
        // ElMessage({
        //   message: "服务器错误",
        //   type: "error",
        //   duration: 3000,
        // });
      }
    } else {
      // if (sessionStorage.getItem("fjpskgToken")) {
      //   let nowStr = Date.parse(new Date()) / 1000;
      //   let jwtData = jwt_decode(sessionStorage.getItem("fjpskgToken"));
      //   if (nowStr > jwtData.exp) {
      //     sessionStorage.removeItem("fjpskgToken");
      //     router.push("/");
      //   }
      // } else {
      //   router.push("/");
      // }
    }
    return Promise.reject(err);
  }
);

export default requests;
